package com.tt.aile.item.controller;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.sun.org.glassfish.gmbal.ParameterNames;
import com.tt.aile.item.pojo.SpecGroup;
import com.tt.aile.item.pojo.SpecParam;
import com.tt.aile.item.service.SpecGroupService;
import com.tt.aile.item.service.SpecParamService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.StyledEditorKit;
import java.util.List;

/**
 * @author 86176 shkstrat
 * @date 2020/9/30 - 9:48
 */
@Controller
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    SpecGroupService specGroupService;

    @Autowired
    SpecParamService specParamService;

    //查根据商品分类id,询规格参数组，
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroupByCid(@PathVariable("cid") Long cid){

        List specGruopsList=this.specGroupService.querySpecGroupByCid(cid);
        if (CollectionUtils.isEmpty(specGruopsList)) return ResponseEntity.status(HttpStatus.NOT_FOUND).build();

        return ResponseEntity.ok(specGruopsList);
    }

    //根据是否为搜索性参数 查询参数
    @GetMapping("searchParams")
    public ResponseEntity<List<SpecParam>> querySearchParamsByCid(@RequestParam("cid") Long cid, @RequestParam("bool")Boolean bool){
        //System.out.println("searParams--------------");
        List<SpecParam> specParamsList = this.specParamService.querySearchParamsByCid(cid,bool);
        if (CollectionUtils.isEmpty(specParamsList)) return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
//        for (Object o : specParamsList) {
//            System.out.println(o);
//        }
        return ResponseEntity.ok(specParamsList);
    }


    @PostMapping("group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroup specGroup){
        //System.out.println("---------------------------------"+specGroup);
        this.specGroupService.saveSpecGroup(specGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PutMapping("group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroup specGroup){

        //System.out.println("---------------------------------"+specGroup);
        this.specGroupService.updateNameById(specGroup.getName(),specGroup.getId());
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteSpecGroupById(@PathVariable("id") Long id){

        specGroupService.deleteSpecGroupById(id);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //根据group id查询规格参数名称,比如手机的规格 cpu核数
    //根据tb_spec_group中的gid(唯一) 查询 tb_spec_param中所有匹配的gid
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParamByGid(@RequestParam("gid") Long gid){

        List<SpecParam> specParams=specParamService.querySpecParamByGid(gid);
        if (CollectionUtils.isEmpty(specParams)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(specParams);
    }

    //跟据cid查询规格参数 比如手机76 我就查询所有手机的规格参数
    @GetMapping("params/goodsAdd")
    public ResponseEntity<List<SpecParam>> querySpecParamByCid(@RequestParam("cid") Long cid){

//        System.out.println("goodsAdd被调用l");
        List<SpecParam> specParams=specParamService.querySpecParamByCid(cid);
        if (CollectionUtils.isEmpty(specParams)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(specParams);
    }



}
